<template>
  <div class="main_box">
    <CommonHeader title="指标排名"></CommonHeader>
    <div class="search_box">
      <van-field
        v-model="queryInfo.quotaName"
        input-align="left"
        name="title"
        label=""
        placeholder="请输入指标名称"
        maxlength="32"
        size="small"
        style="border: 1px solid #efefef; padding: 0 0 0 10px; border-radius: 5px; overflow: hidden">
        <van-button slot="button" size="small" icon="search" type="info" @click="searchQuota">搜索</van-button>
      </van-field>
    </div>
    <div class="nav_box">
      <div class="nav_content">
        <div class="nav_item" :class="item.value == queryInfo.state ? 'active' : ''" v-for="item in selectList" :key="item.value" @click="changeNav(item)">
          {{ item.text }}
        </div>
      </div>
    </div>
    <div class="main_content">
      <div class="nav_wrap">
        <div class="tit" style="width: 156px" v-if="$route.query.level == 2 && selectName == '人员'">辖区/负责人：</div>
        <div class="content" style="width: 180px;" @click="showPersonSelect" v-if="$route.query.level == 2 && selectName == '人员'">
          <div style="max-width: 80px">{{ selectArea }}</div>
          <i class="down"></i>
        </div>
        <div class="tit">选择时间：</div>
        <div class="content" @click="showSelectDate">
          <div>{{ currentDate }}</div>
          <i class="down"></i>
        </div>
      </div>
      <div class="main_list">
        <pull-refresh-list ref="PullRefreshList" :limit="queryInfo.size" :page-no.sync="queryInfo.current" :data.sync="dataList" @getList="initData">
          <div v-for="item in dataList" :key="item.index" class="quota_item">
            <div class="head">
              <div class="tit">{{ item.quotaName }}</div>
              <div class="inx">
                <div class="num1" :class="formateSignage(item.signage)">
                  {{ item.position }}
                  <span class="grey">名</span>
                </div>
                <div class="num2">
                  <div class="up" v-if="item.prePosition !== 0 && item.position - item.prePosition < 0">
                    <i class="icon"></i>
                    {{ item.prePosition - item.position }}
                  </div>
                  <div class="down" v-if="item.prePosition !== 0 && item.position - item.prePosition > 0">
                    <i class="icon"></i>
                    {{ item.position - item.prePosition }}
                  </div>
                  <div class="no" v-if="item.prePosition === 0 || item.position - item.prePosition === 0">- -</div>
                </div>
              </div>
            </div>
            <div class="info" v-if="$route.query.level != '2'">
              <div v-for="desc in item.dimensionArr" :key="desc.index">{{ desc.name }}：{{ desc.value }}</div>
            </div>
            <div class="leader_info" v-else>
              <div class="tit">指标维度</div>
              <div class="content">
                <span v-for="desc in item.dimensionArr" :key="desc.index">{{ desc.name }}</span>
              </div>
            </div>
            <div class="footer">
              <div class="person">
                <span v-if="$route.query.level != '0'">
                  <span v-for="obj in item.empList" :key="obj.index">{{ obj }}</span>
                </span>
              </div>
              <div class="btn" @click.stop="toRankDetail(item)">
                查看全市排名
                <i class="icon"></i>
              </div>
            </div>
          </div>
        </pull-refresh-list>
      </div>
    </div>
    <van-popup v-model="dateShow" round position="bottom" get-container="#app">
      <van-datetime-picker type="year-month" :value="now" @confirm="onConfirm" @cancel="cancel" :min-date="new Date('2022/1/1')" />
    </van-popup>
    <van-popup v-model="personShow" round position="bottom" get-container="#app">
      <van-picker show-toolbar :columns="personList" confirm-button-text="确定" cancel-button-text="取消" @confirm="confirmPerson" @cancel="canclePerson" />
    </van-popup>
    <van-popup v-model="selectShow" round position="bottom" get-container="#app">
      <van-picker show-toolbar :columns="areaSelect" confirm-button-text="确定" cancel-button-text="取消" @confirm="confirmSelect" @cancel="cancleSelect" />
    </van-popup>
  </div>
</template>

<script>
import CommonHeader from '@/components/commonHeader';
import PullRefreshList from '@/components/PullRefreshList';
import { Toast } from 'vant';
export default {
  // eslint-disable-next-line
  name: 'Quota',
  components: {
    CommonHeader,
    PullRefreshList,
  },
  data() {
    return {
      selectList: [
        {
          text: '月度(季度)',
          value: '1',
        },
        {
          text: '年度',
          value: '2',
        },
      ],
      queryInfo: {
        quotaName: '',
        state: '1',
        current: 1,
        size: 10,
      },
      dateShow: false,
      now: '',
      currentDate: '',
      dataList: [], // 列表数据实体
      selectName: '人员',
      personShow: false,
      personList: [],
      selectShow: false,
      areaSelect: ['人员', '县（市、区）'],
      selectArea: '鹿城区',
      areaId: '',
      employId: '',
    };
  },
  computed: {
    formateSignage() {
      return function (id) {
        switch (id) {
          case 0:
            return 'green';
          case 1:
            return 'red';
          default:
            return '';
        }
      };
    },
  },
  created() {
    const year = this.$route.query.year;
    const month = this.$route.query.month;
    this.currentDate = year + '-' + month;
    this.now = new Date(this.currentDate);
    this.areaId = this.$route.query.areaId || sessionStorage.getItem('areaId');
    this.employId = this.$route.query.level == 0 ? sessionStorage.getItem('employId') : '';
    if (this.$route.query.level == 2) {
      this.getAreaPersonList();
    }
  },
  methods: {
    changeNav(data) {
      if (data.value === '2') {
        Toast.fail('该功能暂未开放！');
        return;
      }
      this.queryInfo.state = data.value;
      this.$refs.PullRefreshList.onSearch();
    },
    searchQuota() {
      this.$refs.PullRefreshList.onSearch();
    },
    showSelectDate() {
      this.dateShow = true;
    },
    onConfirm(params) {
      const year = params.getFullYear();
      const month = params.getMonth() + 1;
      // const month = (params.getMonth() + 1) > 9?(params.getMonth() + 1):'0' + (params.getMonth() + 1)
      this.currentDate = year + '-' + month;
      this.dateShow = false;
      this.$refs.PullRefreshList.onSearch();
    },
    cancel() {
      this.dateShow = false;
    },
    initData(callback) {
      let date = '';
      if (this.currentDate && this.currentDate != '-') {
        const arr = this.currentDate.split('-');
        const year = arr[0];
        const month = arr[1] / 1;
        date = year + '-' + month;
      }
      const params = {
        _gp: 'quota',
        _mt: 'keyRanking',
        areaId: this.areaId,
        employId: this.employId,
        issueNo: date,
        signage: this.$route.query.signage || '',
        quotaName: this.queryInfo.quotaName,
        page: this.queryInfo.current,
        limit: this.queryInfo.size,
      };
      this.$api.commonApi(params).then((res) => {
        if (res.errno === 200) {
          const rows = res.data.items.map((item) => {
            // 处理dimension数据
            const dimension = item.dimension.slice(1, -2);
            const arr = dimension.split(',');
            const dimensionArr = [];
            for (let i in arr) {
              let obj = {
                name: arr[i].split(':')[0].split('"')[1],
                value: arr[i].split(':')[1].split('"')[1],
              };
              dimensionArr.push(obj);
            }
            return {
              ...item,
              dimensionArr,
            };
          });
          if (callback) callback(rows);
        } else {
          callback([]);
        }
      });
    },
    toRankDetail(item) {
      this.$router.push({
        path: '/quotaDetail',
        query: {
          ...item,
          currentDate: this.currentDate,
        },
      });
    },
    toDetail(data) {
      this.$router.push({
        path: '/QuotaDetailCharts',
        query: {
          ...data,
        },
      });
    },
    getAreaPersonList() {
      const params = {
        _gp: 'employee',
        _mt: 'getEmpByArea',
      };
      this.$api.commonApi(params).then((res) => {
        if (res.errno == 200) {
          this.personList = res.data.map((item) => {
            const arr = item.employeeDOList.map((desc) => {
              return {
                value: desc.empId,
                text: desc.name,
              };
            });
            return {
              value: item.areaId,
              text: item.areaName,
              children: [
                {
                  text: '全部',
                  value: '',
                },
                ...arr,
              ],
            };
          });
          this.areaId = this.personList[0].value;
        }
      });
    },
    showPersonSelect() {
      this.personShow = true;
    },
    confirmPerson(params, inx) {
      this.areaId = this.personList[inx[0]].value;
      this.employId = this.personList[inx[0]].children[inx[1]].value;
      if (params[1] == '全部') {
        this.selectArea = params[0];
      } else {
        this.selectArea = params[0] + '-' + params[1];
      }
      this.personShow = false;
      this.$refs.PullRefreshList.onSearch();
    },
    canclePerson() {
      this.personShow = false;
    },
    showArea() {
      this.selectShow = true;
    },
    confirmSelect(params) {
      this.selectName = params;
      this.selectShow = false;
      this.$refs.PullRefreshList.onSearch();
    },
    cancleSelect() {
      this.selectShow = false;
    },
  },
};
</script>

<style scoped>
.main_box {
  min-height: 100vh;
  background-color: #f4f5fa;
}
.search_box {
  width: 100%;
  padding: 10px;
  box-sizing: border-box;
  background-color: #fff;
}
.nav_box {
  width: 100%;
  background-color: #fff;
}
.nav_content {
  width: 100%;
  display: flex;
}
.nav_content .nav_item {
  width: 49%;
  height: 36px;
  line-height: 36px;
  font-size: 14px;
  color: #999;
  background-color: #fff;
  cursor: pointer;
  position: relative;
  box-sizing: border-box;
}
.nav_content .nav_item.active {
  color: #333;
}
.nav_content .nav_item.active::after {
  display: block;
  content: '';
  width: 50px;
  border-bottom: 2px solid #1b68eb;
  position: absolute;
  left: 50%;
  bottom: 0;
  margin-left: -25px;
}
.main_list {
  width: 100%;
  padding: 10px 15px;
  box-sizing: border-box;
}
.main_content {
  padding: 15px;
}

.nav_wrap {
  width: 100%;
  display: flex;
  margin-bottom: 15px;
}
.nav_wrap .tit {
  width: 120px;
  font-size: 14px;
  color: #999;
  white-space: nowrap;
}
.nav_wrap .content {
  width: 120px;
  font-size: 14px;
  color: #333;
  display: flex;
  line-height: 18px;
}
.nav_wrap .content .down {
  width: 15px;
  height: 8px;
  background: url('../../assets/image/down.png') no-repeat;
  background-size: 100% 100%;
  position: relative;
  left: 10px;
  top: 5px;
}
.quota_item {
  width: 100%;
  background-color: #fff;
  border-radius: 8px;
  margin-bottom: 10px;
  text-align: left;
  padding: 10px 15px;
  box-sizing: border-box;
}
.quota_item .head {
  display: flex;
  justify-content: space-between;
  box-sizing: border-box;
  border-bottom: 1px solid #efefef;
  padding-bottom: 10px;
}
.quota_item .head .tit {
  max-width: 200px;
  height: 20px;
  font-size: 14px;
  color: #333;
  font-weight: bold;
  border-left: 4px solid #367fff;
  padding-left: 10px;
  box-sizing: border-box;
}
.quota_item .head .inx {
  display: flex;
}
.quota_item .head .num1 {
  font-size: 24px;
  color: #4ca5f1;
  font-weight: bold;
  margin-right: 10px;
}
.quota_item .head .num1 .grey {
  color: #bcc0c7;
  font-size: 13px;
  font-weight: normal;
  vertical-align: middle;
}
.quota_item .head .num1.red {
  color: #e43c26;
}
.quota_item .head .num1.green {
  color: #5bc55a;
}
.quota_item .head .num2 .no {
  font-size: 14px;
  color: #bcc0c7;
  padding-top: 9px;
}
.quota_item .head .num2 .down {
  font-size: 14px;
  color: #e43c26;
  padding-top: 9px;
}
.quota_item .head .num2 .down .icon {
  display: inline-block;
  width: 10px;
  height: 13px;
  background: url('../../assets/image/arrow_down.png') no-repeat;
  background-size: 100% 100%;
  position: relative;
  top: 2px;
  margin-right: 5px;
}
.quota_item .head .num2 .up {
  font-size: 14px;
  color: #5bc55a;
  padding-top: 9px;
}
.quota_item .head .num2 .up .icon {
  display: inline-block;
  width: 10px;
  height: 13px;
  background: url('../../assets/image/arrow_up.png') no-repeat;
  background-size: 100% 100%;
  position: relative;
  top: 2px;
  margin-right: 5px;
}
.quota_item .info,
.quota_item .leader_info {
  display: flex;
  flex-wrap: wrap;
  margin-top: 15px;
  font-size: 14px;
  color: #bcc0c7;
}
.quota_item .leader_info .tit {
  color: #333;
  margin-right: 10px;
}
.quota_item .info > div {
  width: 50%;
  margin-bottom: 10px;
}
.quota_item .footer {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
  font-size: 14px;
  color: #bcc0c7;
  border-top: 1px solid #efefef;
  padding-top: 10px;
}
.quota_item .footer .person {
  font-size: 13px;
  max-width: 50%;
  /* color:#666; */
}
.quota_item .footer .btn {
  color: #4180f2;
  cursor: pointer;
}
.quota_item .footer .btn .icon {
  display: inline-block;
  width: 8px;
  height: 12px;
  background: url('../../assets/image/blue_right.png') no-repeat;
  background-size: 100% 100%;
  position: relative;
  top: 1px;
  margin-left: 5px;
}
</style>
